import pandas as pd
import ollama
from openai import OpenAI
from sklearn.metrics import accuracy_score
import csv
import pandas
import ollama
from pandasai.llm.local_llm import LocalLLM
from pandasai import SmartDataframe
import numpy as np

# Function to read a CSV file, decode it, and return a list of rows
def read_and_decode_csv(file_path, encoding='ISO-8859-1', skip_header=False):
    with open(file_path, 'rb') as f:
        content = f.read().decode(encoding)  # Decode the file content using specified encoding
    # Use the csv.reader to parse the content and convert it to a list
    csv_data = [row for row in csv.reader(content.splitlines())]
    
    # If skipping the header, return data without the first row
    if skip_header:
        return csv_data[1:]
    return csv_data

# Read and decode all files
data_0 = read_and_decode_csv('data/export_179004_0.csv')  # Read the first file (with header)
data_1 = read_and_decode_csv('data/export_179004_1.csv')  
data_2 = read_and_decode_csv('data/export_179004_2.csv')

#concatinate all data with CrashID as the key
combined_data = data_0
for row in data_1[1:]:
    for row2 in combined_data[1:]:
        if row[0] == row2[0]:
            combined_data.append(row)
            break
for row in data_2[1:]:
    for row2 in combined_data[1:]:
        if row[0] == row2[0]:
            combined_data.append(row)
            break

# Convert the combined list to a DataFrame
df = pd.DataFrame(combined_data[1:], columns=combined_data[0])  # First row as column names, rest as data

#save the df to csv
df.to_csv('data.csv', index=False)


